package com.avalon.shop.controller;

import com.avalon.core.model.RecordRow;
import com.avalon.shop.service.UserAddressService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/address")
public class UserAddressController {
    private final UserAddressService userAddressService;

    public UserAddressController(UserAddressService userAddressService) {
        this.userAddressService = userAddressService;
    }

    @PostMapping("/get/all")
    public List<Map> getAddressAll(@RequestBody Map<String, Object> param) {
        Integer userId = 0;
        if (param.containsKey("userId")) {
            userId = (Integer) param.get("userId");
        }
        return userAddressService.getAddressAll(userId);
    }

    @PostMapping("/delete")
    public void deleteAddress(@RequestBody Map<String, Object> param) {
        Integer id = 0;
        if (param.containsKey("id")) {
            id = Integer.parseInt(param.get("id").toString());
        }
        userAddressService.deleteAddress(id);
    }

    @PostMapping("/add")
    public void addAddress(@RequestBody Map<String, Object> param) {
        Integer userId = 0;
        if (param.containsKey("userId")) {
            userId = (Integer) param.get("userId");
        }
        String addressDetail = "";
        if (param.containsKey("addressDetail")) {
            addressDetail = (String) param.get("addressDetail");
        }
        String phone = "";
        if (param.containsKey("phone")) {
            phone = (String) param.get("phone");
        }
        String receiverName = "";
        if (param.containsKey("receiverName")) {
            receiverName = (String) param.get("receiverName");
        }
        userAddressService.addAddress(userId, addressDetail, phone, receiverName);
    }

    @PostMapping("/update")
    public void updateAddress(@RequestBody Map<String, Object> param) {
        Integer id = 0;
        if (param.containsKey("id")) {
            id = Integer.parseInt(param.get("id").toString());
        }
        String addressDetail = "";
        if (param.containsKey("addressDetail")) {
            addressDetail = (String) param.get("addressDetail");
        }
        String phone = "";
        if (param.containsKey("phone")) {
            phone = (String) param.get("phone");
        }
        String receiverName = "";
        if (param.containsKey("receiverName")) {
            receiverName = (String) param.get("receiverName");
        }
        userAddressService.updateAddress(id, addressDetail, phone, receiverName);
    }

    @PostMapping("/get/detail")
    public RecordRow getAddressDetail(@RequestBody Map<String, Object> param) {
        Integer id = 0;
        if (param.containsKey("id")) {
            id = Integer.parseInt(param.get("id").toString());
        }
        return userAddressService.getAddressOne(id);
    }
}
